﻿package app.demo.MyTouchApp{
 
    import flash.events.TUIO; // allows to connect to touchlib/tbeta
 
    import flash.display.*;
    import flash.events.*;
	import flash.events.MouseEvent;
    import flash.geom.*; // needed for curPt;
	import flash.display.SimpleButton;
	import flash.net.URLRequest;
	import flash.net.navigateToURL; 
	import flash.display.FrameLabel;
	import fl.controls.Label;
	import fl.controls.Button;
	import fl.controls.*;
	import fl.core.*;
	import flash.display.Sprite;
	import flash.text.*;
	import flash.system.fscommand;
	import flash.system.*;
	import flash.desktop.*;
 
    public class numberOfPlayers extends MovieClip {
 
        private var color:uint = 0xff0000; // color of lines
        private var thickness:Number = 30; // thickness of lines
        private var blobs:Array = new Array(); // blobs we are currently interacting with
        private var lines:Array = new Array(); // array to hold the lines
		private var numButtonWidth:Number = 100;
		private var numButtonHeight:Number = 50;
		private var images:Array = ["darkBackground.png","shapesinahole.png","chooseplayer.png", "playerOne.png", "playerTwo.png"];
		var movie2:MovieClip = new MovieClip();
		var square:Sprite = new Sprite();
		var enable:Boolean;
		
		var myMaster:master;
		var topic:Loader = new Loader();
		var choose:Loader = new Loader();
		var imageBack:Loader = new Loader();
		public var buttonBack:Button;
		public var buttonExit:Button;
		var playerOne:Loader = new Loader();
		var playerTwo:Loader = new Loader();
		
        public function numberOfPlayers(m:master):void {
			
			//fscommand("allowscale", "false");
			//fscommand("fullscreen", "false");
			//Stage.showMenu = false;
			
 			enable=false;
 			myMaster=m;
            //--------connect to TUIO-----------------
            TUIO.init(this,'localhost',3000,'',true);
            trace("MyTouchApp Initialized");
            //---------------------------------------- 
 			
			//Adding the first movie
			//addChild(movie1);
			addChild(movie2);
			
            //addEventListener(TouchEvent.MOUSE_DOWN, downEvent, false, 0, true); // run when finger down
            addEventListener(TouchEvent.MOUSE_UP, upEvent, false, 0, true); // run when finger up
            addEventListener(Event.ENTER_FRAME, updater, false, 0, true); // keep running

			//Background Page:
			imageBack.load(new URLRequest(images[0]));
			imageBack.scaleX=1.4;
			movie2.addChild(imageBack);
			
			//Page Title (Shapes in a hole)
			topic.load(new URLRequest(images[1]));
			topic.x=240;
			topic.scaleX=1.1;
			topic.scaleY=1.1;
			movie2.addChild(topic);
			
			//Sub title (Choose number of players)
			choose.load(new URLRequest(images[2]));
			choose.x=20;
			choose.y=150;
			choose.scaleX=0.7;
			choose.scaleY=0.7;
			movie2.addChild(choose);
			
			//Back Button:
			buttonBack = new Button();
			buttonBack.label="BACK";
			buttonBack.width=100;
			buttonBack.height=40;
			buttonBack.addEventListener(TouchEvent.MOUSE_DOWN,backMethod);
			movie2.addChild(buttonBack);
			
			//Exit Button:
			buttonExit = new Button();
			buttonExit.x=900;
			buttonExit.y=650;
			buttonExit.width=100;
			buttonExit.height=40;
			buttonExit.label="EXIT";
			buttonExit.addEventListener(TouchEvent.MOUSE_DOWN,closeApp);
			movie2.addChild(buttonExit);
			
			//Adding the pictures of  1 and 2 Players:
			//Player One:
			playerOne.load(new URLRequest(images[3]));
			playerOne.x=200;
			playerOne.y=300;
			playerOne.scaleX=0.8;
			playerOne.scaleY=0.8;
			movie2.addChild(playerOne);
			playerOne.addEventListener(TouchEvent.MOUSE_DOWN,goToAvatar2);
			
			//Player Two:
			playerTwo.load(new URLRequest(images[4]));
			playerTwo.x=600;
			playerTwo.y=300;
			playerTwo.scaleX=0.8;
			playerTwo.scaleY=0.8;
			playerTwo.addEventListener(TouchEvent.MOUSE_DOWN,goToAvatarPlayerOne);
			movie2.addChild(playerTwo);
		
        }
		
		
		
		////////////////////////////////////////////////////////////////
		//Back Method (When back is clicked return to the previous page)
		private function backMethod(e:TouchEvent):void
		{
			myMaster.goBackShapesMain();
		}
		
		////////////////////////////////////////////////////////////////
		function closeApp(event:TouchEvent):void
		{
     		 fscommand("quit");    
     	}
		
		private function goToAvatar2(event:TouchEvent):void
		{
			myMaster.goToAvatarTwo();
		}
		
		private function goToAvatarPlayerOne(event:TouchEvent):void
		{
			myMaster.goToAvatarPlayer1();
		}
		
		
 
        // updates/draws the drawn lines by:
        // cycling through all existing blobs
        // getting the id of each blob
        // use that id to get the line, as the lines are named after their blobs (eg line1280 )
        // tuioobj is the blob - see how it is called by request using the blob id?
        // if it is not there remove it from the blobs array.
        // draw the next segment of the line using lineTo and current loc (tuioobj.x, tuioobj.y)
        function updater(e:Event):void {
            for (var i:int = 0; i < blobs.length; i++) { 
                var tuioobj:TUIOObject=TUIO.getObjectById(blobs[i].id);
 
                // if not found, then it must have died..
                if (! tuioobj) {
                    removeBlob(blobs[i].id);
                } else if (parent != null) {
 
                    lines["line"+blobs[i].id].graphics.lineTo(tuioobj.x, tuioobj.y);
                }
            }
        }
		/*function start():void{
			var aButton:SimpleButton=new SimpleButton;
			

			aButton.addEventListener(MouseEvent.CLICK, clickHandler);
			//aButton.label = "Submit";
			addChild(aButton);
		}*/
 
        // when touched, this calls addblob with the blob ID and the X/Y of where it started
        public function downEvent(e:TouchEvent):void {
            var curPt:Point=parent.globalToLocal(new Point(e.stageX,e.stageY));
            addBlob(e.ID, curPt.x, curPt.y);
            e.stopPropagation();
        }
		
		function clickHandler(evtObj:MouseEvent){
		trace("The " + evtObj.target.label + " button was clicked");
		}
 
        // When a finger stops touching, call removeBlob
        public function upEvent(e:TouchEvent):void {
            removeBlob(e.ID);
            e.stopPropagation();
        }
 
        // This takes the sent ID, x,y and puts it all in the blobs array
        // It then creats a new sprite, and sets the starting loc of the line
        // The line is put in an array called lines, so it can be called over and over by name
        // if the lines did not have unique names you could only draw one line at a time
        // so each line is named in the array after the id of the blob (EG line1280)
        function addBlob(id:Number, origX:Number, origY:Number):void {
            blobs.push( {id: id, origX: origX, origY: origY, myOrigX: x, myOrigY:y} );
 
            var line:Sprite = new Sprite();
            lines["line"+id] = line;
            lines["line"+id].graphics.lineStyle(thickness, color); //<-- config above
            lines["line"+id].graphics.moveTo(origX, origY); //starting point of line to be drawn
            addChild(lines["line"+id]); // add line to stage
        }
 
        // when called, this function removes the blobs from the array
        // cycles through the blobs array until the blob is found by its ID, then it is removed.
        function removeBlob(id:Number):void {
            for (var i=0; i < blobs.length; i++) {
                if (blobs[i].id==id) {
                    blobs.splice(i, 1);
                    return;
                }
            }
        }
 
    }
}